package com.itheima.health.controller;

import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("role")
public class RoleController {
	@DubboReference
	RoleService roleService;

	@GetMapping("findMenuIdsById")
	public Result findMenuIdsById(Integer id) {
		List<Integer> menuIds=roleService.findMenuIdsById(id);
		return new Result(true, "",menuIds);
	}
/**
 * @Author: wty
 * @Date: 12:06 2021/3/12
 * @Parms [permissionsIds, role]
 * @ReturnType: com.itheima.health.entity.Result
 * @Description: 更新用户角色
*/
	@PostMapping("update")
	@PreAuthorize("hasAnyAuthority('ROLE_EDIT')")
	public Result update(Integer[] permissionsIds,Integer[] menuIds,@RequestBody Role role) {
		roleService.update(permissionsIds,menuIds, role);
		return new Result(true, "更新成功");
	}
/**
 * @Author: wty
 * @Date: 12:04 2021/3/12
 * @Parms [id]
 * @ReturnType: com.itheima.health.entity.Result
 * @Description: 查询所有的用户权限
*/
	@GetMapping("findpermissionsIdsById")
	@PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
	public Result findpermissionsIdsById(Integer id) {
		List<Integer> permissionsIds = roleService.findpermissionsIdsById(id);
		return new Result(true, "", permissionsIds);
	}
/**
 * @Author: wty
 * @Date: 11:43 2021/3/12
 * @Parms [id]
 * @ReturnType: com.itheima.health.entity.Result
 * @Description: 角色回显
*/
	@GetMapping("findById")
	@PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
	public Result findById(Integer id) {
		log.info("[角色id]id:{}",id);
		Role role = roleService.findById(id);
		return new Result(true, "", role);
	}
/**
 * @Author: wty 
 * @Date: 18:11 2021/3/11
 * @Parms []
 * @ReturnType: com.itheima.health.entity.Result
 * @Description: 查询所有角色
*/
	@GetMapping("findAllRoles")
	@PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
	public Result findAllRoles() {
	List<Role>roleList= roleService.findAllRoles();
		return new Result(true, "", roleList);
	}
/**
 * @Author: wty 
 * @Date: 18:12 2021/3/11
 * @Parms [queryPageBean]
 * @ReturnType: com.itheima.health.entity.Result
 * @Description: 
*/
	@GetMapping("findPage")
	@PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
	public Result findPage(QueryPageBean queryPageBean) {
		PageResult pageResult = roleService.findPage(queryPageBean);
		return new Result(true, "查询所有角色成功", pageResult);
	}
/**
 * @Author: wty 
 * @Date: 18:12 2021/3/11
 * @Parms [permissionsIds, role]
 * @ReturnType: com.itheima.health.entity.Result
 * @Description: 添加用户
*/
	@PostMapping("add")
	@PreAuthorize("hasAnyAuthority('ROLE_ADD')")
	public Result add(Integer[] permissionsIds,Integer[] menuIds,@RequestBody Role role) {
		roleService.add(permissionsIds,menuIds, role);
		return new Result(true, "添加角色成功");
	}
/**
 * @Author: wty 
 * @Date: 18:13 2021/3/11
 * @Parms [id]
 * @ReturnType: com.itheima.health.entity.Result
 * @Description: 删除用户
*/
	@GetMapping("delete")
	@PreAuthorize("hasAnyAuthority('ROLE_DELETE')")
	public Result delete(Integer id) {
		roleService.delete(id);
		return new Result(true, "删除成功");
	}
}
